﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using TaskManagerLib.ComplexWork;
using TaskManagerLib.MinimumWork;
using TaskManagerLib.Test;

namespace JobTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // Task.Run(async () =>
            // {
            //     var path = @"C:\Users\betha\RiderProjects\JobManager\TaskManagerLib\source\tmp.txt";
            //     var download = new DownloadTask("https://tmp.atpgroup.work/download/file/911071968/rocks-8208314.jpg",
            //         path);
            //     download.Notification += notification =>
            //     {
            //         Console.WriteLine($"Notification: Progress {notification.Progress:F2}");
            //         Dispatcher.Invoke(() =>
            //         {
            //             Box.Text += $"Notification: Progress {notification.Progress:F2}\n";
            //         });
            //     };
            //     download.Build();
            //     await download.StartAndBlockAsync();
            // });
        }

        public CancellationTokenSource Source
        {
            get;
            set;
        }

        public ContinuesDownloadWork DownloadWork
        {
            get;
            set;
        }

        public ComplexWork? ComplexWork
        {
            get;
            set;
        }

        private async void Start_Click(object sender, RoutedEventArgs e)
        {
            // Source = new CancellationTokenSource();
            // // var url = @"https://tmp.atpgroup.work/download/file/1993659205/tmp4.jpeg";
            // var url = @"https://tmp.atpgroup.work/download/file/-262447586/fontawesome-free-6.4.2-web.zip";
            // // var url = @"https://tmp.atpgroup.work/download/file/1412741400/cqz04-niwyt_rof_7k1.pdf";
            // // var url = @"https://tmp.atpgroup.work/download/file/-1455431979/fontawesome-free-6.4.2-web.zip";
            // // var url = @"https://download.jetbrains.com/rider/JetBrains.Rider-2023.2.1.exe?_gl=1*1dby4kl*_ga*MTg3Mzk0OTkyMi4xNjkwNjgyMDEz*_ga_9J976DJZ68*MTY5NTcwNzczOC41LjEuMTY5NTcwNzgyNy40NS4wLjA.&_ga=2.165514426.422084669.1695707738-1873949922.1690682013";
            // var path = @"C:\Users\betha\RiderProjects\JobManager\TaskManagerLib\source\tmp45.zip";
            // // DownloadWork = new ContinuesDownloadWork("https://tmp.atpgroup.work/download/file/-2049452763/fontawesome-free-6.4.2-web.zip",
            // //     path,Source);
            // DownloadWork = new ContinuesDownloadWork(url, path, Source);
            // DownloadWork.Notification += notification =>
            // {
            //     Dispatcher.Invoke(() =>
            //     {
            //         // Box.Text += $"Notification: Progress {notification.Progress:F2}\n";
            //         Box.Text += notification.Message+"\n";
            //         Bar.Value = (notification.Progress??0)*100;
            //     });
            // };
            // DownloadWork.Build();
            // Console.WriteLine(JsonConvert.SerializeObject(DownloadWork));
            // await DownloadWork.StartAndBlockAsync();
            var source = new CancellationTokenSource();
            var down = new ContinuesDownloadWork(@"https://tmp.atpgroup.work/download/file/-262447586/fontawesome-free-6.4.2-web.zip",
                @"C:\Users\betha\Desktop\MiniTest\cc.zip", source)
            {
                WorkId = 2,
            };
            down.Notification += notification =>
            {
                Console.WriteLine(notification.Message);
            };
            var post = new PostWork(source, "https://translator.atpgroup.work/translate",
                "tr_translator324234", new Translate()
                {
                    Directly = true,
                    Content = $"translate dog into Chinese"
                })
            {
                WorkId = 1,
            };
            ComplexWork = new ComplexWork(source);
            ComplexWork.AddMiniWork(post);
            ComplexWork.AddMiniWork(down);
            ComplexWork.Notification += notification =>
            {
                Box.Text += notification.Message+"\n";
                Bar.Value = 100*(notification.Progress ?? 0);
            };
            Source = source;
            await ComplexWork.StartAndBlockAsync();
        }

        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            Source?.Cancel();
        }

        private async void Continues_Click(object sender, RoutedEventArgs e)
        {
            Source = new CancellationTokenSource();
            // Box.Text += "\n has written "+DownloadWork.BytesWritten+"\n";

            // DownloadWork.Resume(Source);
            // await DownloadWork.StartAndBlockAsync();
            // Console.WriteLine("done");
            ComplexWork.UpdateTokenSource(Source);
            await ComplexWork?.StartAndBlockAsync();
        }

        private async void Test_OnClick_Click(object sender, RoutedEventArgs e)
        {
            foreach (var miniWork in ComplexWork?.MiniWorks)
            {
                Box.Text += miniWork.Status;
                Box.Text += miniWork.Result;
            }
            // Console.WriteLine(await DownloadWork.Checkout());
        }
    }
}